package com.sukaiyi.generator.config;

import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author sukaiyi
 */
public class Configurable implements SearchableConfigurable, SearchableConfigurable.Composite {

    private final PluginConfig pluginConfig = PluginConfig.getInstance();

    private JPanel rootPanel;

    private JCheckBox generateLombokVO;
    private JCheckBox voJsonFormat;

    public Configurable() {
    }

    @NotNull
    @Override
    public String getId() {
        return Configurable.class.getName();
    }

    @Nls
    @Override
    public String getDisplayName() {
        return "Code Generator";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        generateLombokVO.setSelected(pluginConfig.generateLombokVO);
        voJsonFormat.setSelected(pluginConfig.voJsonFormat);
        return rootPanel;
    }

    @Override
    public boolean isModified() {
        return !(generateLombokVO.isSelected() == pluginConfig.generateLombokVO &&
                voJsonFormat.isSelected() == pluginConfig.voJsonFormat
        );
    }

    @Override
    public void apply() throws ConfigurationException {
        pluginConfig.generateLombokVO = generateLombokVO.isSelected();
        pluginConfig.voJsonFormat = voJsonFormat.isSelected();
    }

    @NotNull
    @Override
    public com.intellij.openapi.options.Configurable[] getConfigurables() {
        SearchableConfigurable[] configurables = new SearchableConfigurable[1];
        configurables[0] = new TemplateConfigurable(pluginConfig);
        return configurables;
    }
}
